define(function (require, exports, module) { 
(function($) {    
   $.fn.resize = function() {  
       
            return this.each(function(){
                var currentDisTance = 5;  
                var resizeMode = 0;  
                var active=false;  
                var isStartResize=false;  
       
                var $this = $(this);
                var $parent = $this.offsetParent();
                var $document = $(document);
       
                var startX, startY;
                
                var oldWidth;  
                var oldHeight;  
              
                $this.on("mousemove", function(e) {                          
                    if(isStartResize==true) return;    
                    
                    if ($this.width() - e.offsetX < currentDisTance*2 && ($this.height() - e.offsetY) < currentDisTance*2) {  
                        $this.css("cursor", "nw-resize");  
                      
                    } else if ($this.width() - e.offsetX < currentDisTance*2) {  
                        $(this).css("cursor", "e-resize");  
                          
                    } else if ($this.height() - e.offsetY < currentDisTance) {  
                        $this.css("cursor", "n-resize");  
                      
                    } else {  
                        if(isStartResize==true)return;  
                        $this.css("cursor", "default");  
                    }  
                });
       
                $this.on("mousedown", function(e) {  
                    active = true;  
                    startX = e.pageX - $this.offset().left;  
                    startY = e.pageY - $this.offset().top;  
                    
                    if ($this.width() - e.offsetX < currentDisTance*2 && ($this.height() - e.offsetY) < currentDisTance*2) {                       
                        $this[0].resize = true;
                        resizeMode = 0;  
                    } else if ($this.width() - e.offsetX < currentDisTance*2 ) {                         
                        $this[0].resize = true;
                        resizeMode = 1;  
                    } else if ($this.height() - e.offsetY < currentDisTance) {                        
                        $this[0].resize = true;                       
                        resizeMode = 2;  
                    } else {  
                        if(isStartResize==true) return;                        
                        $this[0].resize = false;  
                    }                    
                      
                    oldWidth= $this.width()- $this.offset().left;  
                    oldtHeight= $this.height()-$this.offset().top;    
                    
                    $document.on("mousemove", startMove);                                
                    return true;
                });  
                
                function startMove(event){                  
                    if (event.which != 1) {
                        active = false;   
                        isStartResize = false
                    }
                    
                    if (event.pageX > $parent.offset().left + $parent.width() || event.pageY > $parent.offset().top + $parent.height()){
                        return;
                    }
                    
                    if (active && $this[0].resize){                            
                        isStartResize=true;                             
                        var cursorX = event.pageX - startX;   
                        var cursorY = event.pageY - startY;   
                       
                        var newWidth = cursorX+oldWidth;
                        var newHeight = cursorY+oldtHeight;                        
                       
                        var maxWidth = $parent.offset().left + $parent.width - $this.offset().left;
                        var maxHeight = $parent.offset().top + $parent.height() - $this.offset().top;
                        
                        if (newWidth > maxWidth) newWidth = maxWidth;
                        if (newHeight > maxHeight) newHeight = maxHeight;                         
                    
                        if (resizeMode == 0) $this.height(newHeight).width(newWidth);  
                        else if (resizeMode == 1) $this.width(newWidth);  
                        else $this.height(newHeight);  
                        
                        var e = jQuery.Event("resize");
                        e.newWidth = newWidth;
                        e.newHeight = newHeight;
                        $this.trigger(e);
                    }
                };       
               
                
                $document.on("mouseup", function() {  
                    $document.off("mousemove", startMove);
                    active = false;  
                    isStartResize=false;  
                });  
            })                             
        };  
})(jQuery)
})